package com.hongning.railway.ui.main.fragments.threesessionsandonelesson;

import android.app.Application;
import android.databinding.ObservableArrayList;
import android.databinding.ObservableField;
import android.databinding.ObservableInt;
import android.databinding.ObservableList;
import android.support.annotation.NonNull;
import android.view.View;

import com.hongning.railway.BR;
import com.hongning.railway.R;
import com.hongning.railway.app.Constant;
import com.hongning.railway.data.AppRepository;
import com.hongning.railway.entity.reqbean.MettingMessageBean;
import com.hongning.railway.entity.reqbean.ThreesessionSanDoneLessonBean;
import com.hongning.railway.entity.resbean.BaseRes;
import com.hongning.railway.entity.resbean.ResVideoBean;
import com.hongning.railway.ui.main.fragments.college.LearnCompleteFragmentViewModel;
import com.hongning.railway.ui.main.fragments.college.itemviewModel.LessonItemViewModel;
import com.hongning.railway.ui.main.fragments.college.itemviewModel.VideoItemViewModel;
import com.hongning.railway.ui.main.fragments.home.itemviewmodel.MineItemViewModel;
import com.hongning.railway.ui.main.fragments.home.itemviewmodel.ThreesessionSanDoneLessonItemViewModel;
import com.hongning.railway.ui.uichangeobserable.ListUIChangeObserable;
import com.hongning.railway.util.Apputils;

import io.reactivex.disposables.Disposable;
import io.reactivex.functions.Action;
import io.reactivex.functions.Consumer;
import me.goldze.mvvmhabit.base.BaseViewModel;
import me.goldze.mvvmhabit.base.MultiItemViewModel;
import me.goldze.mvvmhabit.binding.command.BindingAction;
import me.goldze.mvvmhabit.binding.command.BindingCommand;
import me.goldze.mvvmhabit.http.ApiDisposableObserver;
import me.goldze.mvvmhabit.http.BaseResponse;
import me.goldze.mvvmhabit.utils.RxUtils;
import me.goldze.mvvmhabit.utils.SPUtils;
import me.goldze.mvvmhabit.utils.ToastUtils;
import me.tatarka.bindingcollectionadapter2.ItemBinding;

public class ThreesessionSanDoneLessonViewModel extends BaseViewModel<AppRepository> {
    private int page=1;
    public ObservableList<ThreesessionSanDoneLessonItemViewModel> viewlist=new ObservableArrayList<>();

    public ObservableInt liststatus = new ObservableInt(View.GONE);
    public ItemBinding<ThreesessionSanDoneLessonItemViewModel> itemBinding = ItemBinding.of(BR.thressItemviewModel, R.layout.activity_threesessionsandonelesson_item);
    public ListUIChangeObserable uc = new ListUIChangeObserable();
    private MettingMessageBean mettingMessageBean=new MettingMessageBean();
    public ObservableField<String> rightContent = new ObservableField<>("最新纪要");
    public BindingCommand onRefreshCommand = new BindingCommand(new BindingAction() {
        @Override
        public void call() {
            page=1;
            mettingMessageBean.setPagenum(String.valueOf(page));
           initDatas(mettingMessageBean);
        }
    });

    public BindingCommand onLoadMoreCommand = new BindingCommand(new BindingAction() {
        @Override
        public void call() {
            mettingMessageBean.setPagenum(String.valueOf(page++));
            initDatas(mettingMessageBean);
        }
    });
    public ThreesessionSanDoneLessonViewModel(@NonNull Application application, AppRepository model) {
        super(application, model);
    }

    /**
     * 按钮点击事件
     * @param id
     */
    public void click(int  id){
        switch (id)
        {
            case 0:
                finish();
                break;
            case 1:
                viewlist.clear();
                uc.finishRefresh.call();
                mettingMessageBean.setPagenum(String.valueOf(1));
              if (rightContent.get().equals("最新纪要"))
              {
                  mettingMessageBean.setSigncolumn("2");
                  rightContent.set("全部");

              }else {
                  mettingMessageBean.setSigncolumn("");
                  rightContent.set("最新纪要");
              }
              initDatas(mettingMessageBean);
                break;
            case 2:
                //退出登录
                showDialog();
                break;

        }
    }

    public void initDatas(MettingMessageBean mettingMessageBean){
        mettingMessageBean.setUserid(Apputils.getUserId());
        model.selectmeet(mettingMessageBean)
                .compose(RxUtils.schedulersTransformer()) //线程调度
                .compose(RxUtils.bindToLifecycle(getLifecycleProvider()))
                .doOnSubscribe(new Consumer<Disposable>() {
                    @Override
                    public void accept(Disposable disposable) throws Exception {
                        showDialog();
                    }
                }).doOnTerminate(new Action() {
            @Override
            public void run() throws Exception {
                dismissDialog();
            }
        }).subscribe(new ApiDisposableObserver<ThreesessionSanDoneLessonBean>() {
            @Override
            public void onResult(ThreesessionSanDoneLessonBean response) {
                if (page==1){
                    viewlist.clear();
                    uc.finishRefresh.call();
                    if (response==null||response.getData()==null||response.getData()==null||response.getData().isEmpty()){
                        uc.contentEmpty.call();
                        liststatus.set(View.VISIBLE);
                        return;
                    }else {
                        liststatus.set(View.GONE);
                    }
                }else {
                    uc.finishLoadMore.call();
                }
                if (response.getData().size()<Constant.PAGE_SIZE){
                    uc.finishLoadMoreEnd.call();
                }
                page++;

                for (ThreesessionSanDoneLessonBean.DataBean dataBean:response.getData()) {
                    viewlist.add(new ThreesessionSanDoneLessonItemViewModel(ThreesessionSanDoneLessonViewModel.this,dataBean));
                }
            }
        });
    }
}
